package com.chen.controller.user;

import com.chen.common.result.Result;
import com.chen.common.utils.JwtUtil;
import com.chen.mapper.ContestLeaderboardMapper;
import com.chen.popj.dto.ContestProblemSubmitDTO;
import com.chen.popj.vo.ContestCodeSubmitVO;
import com.chen.popj.vo.ContestLeaderboardVO;
import com.chen.popj.vo.ContestProblemVO;
import com.chen.popj.vo.ContestVO;
import com.chen.service.ContestLeaderboardService;
import com.chen.service.ContestService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.websocket.server.PathParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController(value = "userContestController")
@RequestMapping("/user")
public class ContextController {


    @Autowired
    ContestService contestService;

    @Autowired
    HttpServletRequest httpServletRequest;

    @Autowired
    ContestLeaderboardService contestLeaderboardService;

    /**
     * 根据比赛id和用户id获取比赛信息
     *
     * @param contestId
     * @return
     */
    @GetMapping("/contest/acm/{contestId}")
    public Result<ContestVO> getContestInfo(@PathVariable Integer contestId) {
        ContestVO contestVO = contestService.getContestInfo(contestId);
//        log.info(contestVO.toString());
        return Result.success(contestVO);
    }

    /**
     * 根据比赛id和题目id获取比赛题目信息
     *
     * @param problemId
     * @param contestId
     * @return
     */
    @GetMapping("/contestProblemDetails")
    public Result<ContestProblemVO> getContestProblemDetails(
            @RequestParam("problemId") Integer problemId,
            @RequestParam("contestId") Integer contestId) {
        ContestProblemVO contestProblemVO = contestService.getContestProblemDetails(problemId, contestId);
        return Result.success(contestProblemVO);
    }

    /**
     * 提交比赛题目代码
     *
     * @param contestProblemSubmitDTO
     * @return
     */
    @PostMapping("contestProblemSubmit")
    public Result ContestProblemSubmit(@RequestBody ContestProblemSubmitDTO contestProblemSubmitDTO) {
        System.out.println(contestProblemSubmitDTO);
        Integer contestProblemSubmit = contestService.ContestProblemSubmit(contestProblemSubmitDTO);
        return Result.success(contestProblemSubmit);
    }

    /**
     * 比赛时查询提交记录
     *
     * @param contestId
     * @return
     */
    @PostMapping("/contest/submitRecord/{contestId}")
    public Result<List<ContestCodeSubmitVO>> getSubmissionRecord(
            @PathVariable(value = "contestId") Integer contestId) {
        String authorization = httpServletRequest.getHeader("Authorization");
        Map<String, Object> parseToken = JwtUtil.parseToken(authorization);
        Integer userId = (Integer) parseToken.get("id");
//        System.out.println(contestId);
//        System.out.println(userId);
        List<ContestCodeSubmitVO> submissionRecord = contestService.getSubmissionRecord(contestId, userId);
        return Result.success(submissionRecord);
    }

    /**
     * 用户比赛报名
     *
     * @param contestId
     * @return
     */
    @PostMapping("/contest/register/{contestId}")
    public Result<Object> contestRegister(@PathVariable Integer contestId) {
//        log.info("/contest/register/");
        contestService.contestRegister(contestId);
        return Result.success();
    }

    /**
     * 获取比赛排行榜信息
     *
     * @param contestId
     * @return
     */
    @PostMapping("/contest/contestLeaderboard/{contestId}")
    public Result<List<ContestLeaderboardVO>> getContestLeaderboard(@PathVariable Integer contestId) {
        System.out.println("contestId:" + contestId);
        List<ContestLeaderboardVO> contestLeaderboardVOList = contestLeaderboardService.getContestLeaderboard(contestId);
//        System.out.println(contestLeaderboardVOList);
        return Result.success(contestLeaderboardVOList);
    }
}
